package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.UserService;
import com.itheima.ydd.service.YddUserDataService;
import com.itheima.ydd.utils.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/4/10 18:34
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询我的文章
     * @param limit
     * @param page
     * @return
     */
    @GetMapping("/myArticle")
    public ResponseEntity<Result> getMyArticle(String limit,String page,HttpServletRequest request){
        PageDto<YddArticle> myArticle = userService.getMyArticle(limit, page,request);
        return ResponseEntity.ok(new Result(0,"成功",myArticle));
    }

    /**
     *分页查询我的点赞和收藏文章
     * @param type
     * @param limit
     * @param page
     * @return
     */
    @GetMapping("/userDataList")
    public ResponseEntity<Result> getUserDataList(String type,String limit,String page,HttpServletRequest request){

        return userService.getUserDataList(type, limit, page,request);
    }

    /**
     * 修改文章
     * @param article
     * @return
     */
    @PostMapping("/editArticle")
    public ResponseEntity<Result> editArticle(@RequestBody YddArticle article,HttpServletRequest request){
        userService.editArticle(article,request);
        return ResponseEntity.ok(new Result(0,"成功",null));
    }

    /**
     * 点赞/取消点赞和收藏/取消收藏
     * @param type
     * @param article_id
     * @param action
     * @return
     */
    @GetMapping("/userDataHandle")
    public ResponseEntity<Result> userDataHandle( String type,String article_id, String action,HttpServletRequest request){
        userService.userDataHandle(type,article_id,action,request);
        return ResponseEntity.ok(new Result(0,"成功",null));
    }

    /**
     * 修改用户资料
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public ResponseEntity<Result> update(@RequestBody YddUser yddUser,HttpServletRequest request){
        userService.update(yddUser,request);
        return ResponseEntity.ok(new Result(0,"成功"));
    }

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping("/addArticle")
    public ResponseEntity<Result> addArticle(@RequestBody YddArticle article,HttpServletRequest request){
        userService.addArticle(article,request);
        return ResponseEntity.ok(new Result(0,"成功"));
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/delArticle")
    public ResponseEntity<Result> delArticleById(String id,HttpServletRequest request) {
        userService.delArticleById(id,request);
        return ResponseEntity.ok(new Result(0,"成功"));
    }

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    public ResponseEntity<Result> getUserInfo(HttpServletRequest request) {
        YddUser userInfo = userService.getUserInfo(request);
        return  ResponseEntity.ok(new Result(0,"成功",userInfo));
    }
}
